#include "RingQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <iostream>
#include <ctime>
#include <unistd.h>
using namespace std;

void *Consumer(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    while (true)
    {
        Task t;
        rq->Pop(&t);
        t();
        cout << "Consumer->" << t.result() << endl;
    }
}

void *Productor(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    while (true)
    {
        int x = rand() % 10 + 1;
        usleep(x * 10000);
        int y = rand() % 10 + 1;
        Task t(x, y);
        rq->Push(t);
        cout << "Prodctor->" << t.debug() << endl;
    }
}

int main()
{
    srand(time(nullptr) ^ getpid());
    RingQueue<Task> *rq = new RingQueue<Task>(5);
    pthread_t p1, p2, c1, c2, c3;
    pthread_create(&p1, nullptr, Productor, rq);
    pthread_create(&p2, nullptr, Productor, rq);
    pthread_create(&c1, nullptr, Consumer, rq);
    pthread_create(&c2, nullptr, Consumer, rq);
    pthread_create(&c3, nullptr, Consumer, rq);

    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);

    return 0;
}